package com.ovhoo.vdm.vdm;





import java.util.ArrayList;



import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

public class VDMQuotesList extends ListActivity {
   
	public enum requestType  {VDM_QUOTE_TYPE_LATEST, VDM_QUOTE_TYPE_RANDOM, VDM_QUOTE_TYPE_TOP, VDM_QUOTE_TYPE_FLOP};
	

	
	private ProgressDialog waitingDialog;
	
	
	private VDMQuotesListAdapter listAdapter;
	
	
	private ArrayList<VDMQuote> quotes;
	private VDMControler vdmcontroler;
	
	private Intent vdmQuoteCommentsList = null; // Intent used to display the comments of a quote after user asked for

	
	
	Handler handler=new Handler() {
		@Override
		public void handleMessage(Message msg) {
			quotes = (ArrayList<VDMQuote>) msg.obj;
			listAdapter.setQuotes(quotes);
			
			waitingDialog.dismiss();
			
			listAdapter.notifyDataSetChanged();
		}
	};
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.vdmlist);
        
        
        this.listAdapter = new VDMQuotesListAdapter(this);
        setListAdapter(this.listAdapter);
        
        
        //Fenetre d'attente
        this.waitingDialog = ProgressDialog.show(this, this.getResources().getString(R.string.vdm_progresdialog_title), this.getResources().getString(R.string.vdm_progresdialog_text));
        
        
        //Thread : controler
        this.vdmcontroler = VDMControler.getInstance();;
    }
    
    public void onStart(){
    	super.onStart();
    	
    	
    	Bundle extras  = getIntent().getExtras();
        if(extras != null){
        	requestType paramIntent  = int2requestType( extras.getInt("com.ovhoo.vdm.vdm.VDM_QUOTES_LIST_TYPE")); 
        	
        	
        	if (paramIntent == requestType.VDM_QUOTE_TYPE_RANDOM ){
        		this.setTitle(this.getResources().getString(R.string.random_quotes));
        		this.vdmcontroler.makeARequest(VDMDownloadRequest.requestType.LOAD_RANDOM_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        	}
        	else if (paramIntent == requestType.VDM_QUOTE_TYPE_TOP){
        		this.setTitle(this.getResources().getString(R.string.top_quotes));
        		this.vdmcontroler.makeARequest(VDMDownloadRequest.requestType.LOAD_TOP_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        	}
        	else if (paramIntent == requestType.VDM_QUOTE_TYPE_FLOP){
        		this.setTitle(this.getResources().getString(R.string.flop_quotes));
        		this.vdmcontroler.makeARequest(VDMDownloadRequest.requestType.LOAD_FLOP_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        	}
        	else{
        		this.setTitle(this.getResources().getString(R.string.latest_quotes));
        		this.vdmcontroler.makeARequest(VDMDownloadRequest.requestType.LOAD_LATEST_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        	}

        }
        else{
        	this.setTitle(this.getResources().getString(R.string.latest_quotes));
        	this.vdmcontroler.makeARequest(VDMDownloadRequest.requestType.LOAD_LATEST_QUOTES, 0, handler.obtainMessage(), this.getResources().getString(R.string.VDM_API_LANGAGE));
        }
    	
    }
    
    /* Creates the menu items */
    public boolean onCreateOptionsMenu(Menu menu) {
        //menu.add(0, 0, 0, "Menu 1");
        //menu.add(0, 1, 0, "Menu 2");
        return true;
    }

    /* Handles item selections */
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 0:
            return true;
        case 1:
            return true;
        }
        return false;
    }

    public static requestType int2requestType(int param){
    	return requestType.class.getEnumConstants()[param];
    }
    
    /**
     * Called when the user selects an element on the list
     */
    protected void  onListItemClick  (ListView l, View v, int position, long id){
    	super.onListItemClick  (l, v, position,id);
    	this.startVDMQuoteCommentsList(VDMQuoteCommentsList.requestType.LOAD_QUOTE_COMMENTS_BY_ID,this.quotes.get(position).getId());
    }
    
    /**
	 * Start a VDMQuoteCommentsList with the specified parameter
	 * @param type
	 */
	protected void startVDMQuoteCommentsList(VDMQuoteCommentsList.requestType type, int param){
		
		//If the Activity was not already instanciated
		if (this.vdmQuoteCommentsList == null){
			this.vdmQuoteCommentsList = new Intent(this,VDMQuoteCommentsList.class);
		}
				
		this.vdmQuoteCommentsList = this.vdmQuoteCommentsList.putExtra("com.ovhoo.vdm.vdm.VDM_QUOTE_COMMENTS_LIST_TYPE", type.ordinal());
		this.vdmQuoteCommentsList = this.vdmQuoteCommentsList.putExtra("com.ovhoo.vdm.vdm.VDM_QUOTE_COMMENTS_LIST_ID", param);
		
		startActivity(this.vdmQuoteCommentsList);

	}
}